package com.xyd.auth.contoller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyd.auth.service.ISysUserService;
import com.xyd.common.result.GlobalResultClass;
import com.xyd.common.util.MD5;
import com.xyd.model.system.SysUser;
import com.xyd.model.system.vo.SysUserQueryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    ISysUserService sysUserService;
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @Operation(summary = "分页查询（条件）")
    @GetMapping("/{page}/{limit}")
    public GlobalResultClass pageQuerySysUser(@Parameter(name = "page", description = "当前页") @PathVariable Integer page,
                                            @Parameter(name="limit",description = "每页显示多少条记录")  @PathVariable Integer limit,
                                            @Parameter(name = "sysUserQueryVO", description = "条件查询的VO值对象") SysUserQueryVO sysUserQueryVO
                                              ) {

        // 创建page对象
        Page<SysUser> sysUserPage = new Page<>(page,limit);
        // 封装条件对象
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        // 获取条件
        String keyword = sysUserQueryVO.getKeyword();
        String createTimeBegin = sysUserQueryVO.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVO.getCreateTimeEnd();

        // 判断条件不为空
        if(!StringUtils.isEmpty(keyword)){
            // 不为空
            wrapper.like(SysUser::getUsername,keyword).
                    or().like(SysUser::getName,keyword).
                    or().like(SysUser::getPhone,keyword);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            // 大于等于创建用户的开始时间
            wrapper.ge(SysUser::getCreateTime,createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            // 小于等于创建用户的结束时间
            wrapper.le(SysUser::getCreateTime,createTimeEnd);
        }
        // 根据id倒序排列
        wrapper.orderByDesc(SysUser::getId);
        Page<SysUser> page1 = sysUserService.page(sysUserPage, wrapper);
        return GlobalResultClass.ok(page1);
    }

    // 新增用户
    @PreAuthorize("hasAuthority('bnt.sysUser.save')")
    @Operation(summary = "新增用户")
    @PostMapping("/save")
    public GlobalResultClass saveSysUser(@RequestBody SysUser sysUser) {
        //获取用户密码
        String password = sysUser.getPassword();
        // 对密码进行加密
        String encryptPassword = MD5.encrypt(password);
        sysUser.setPassword(encryptPassword);
        sysUserService.save(sysUser);
        return GlobalResultClass.ok();
    }

    // 根据id查询用户
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @Operation(summary = "查询用户")
    @GetMapping("/get/{id}")
    public GlobalResultClass getSysUserById(@PathVariable @Parameter(name = "id") Integer id) {
        SysUser sysUser = sysUserService.getById(id);
        return GlobalResultClass.ok(sysUser);
    }


    // 修改用户
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @Operation(summary = "修改用户")
    @PutMapping("/update")
    public GlobalResultClass updateSysUserById(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return GlobalResultClass.ok();
    }

    // 删除
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @Operation(summary = "删除用户")
    @DeleteMapping("/remove/{id}")
    public GlobalResultClass deleteSysUserById(@PathVariable @Parameter(name = "id") Integer id) {
        sysUserService.removeById(id);
        return GlobalResultClass.ok();
    }
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    // 批量删除
    @Operation(summary = "批量删除")
    @DeleteMapping("/removes")
    public GlobalResultClass deleteBatchSysUserByIds(@RequestBody Integer[] ids) {
        sysUserService.removeBatchByIds(Arrays.asList(ids));
        return GlobalResultClass.ok();
    }
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @Operation(summary = "跟新状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public GlobalResultClass updateStatus(@PathVariable Long id,@PathVariable Integer status){
        sysUserService.updateStatus(id,status);
        return GlobalResultClass.ok();
    }


}
